<?php
/**
 * This is main controller for LilPlugins. Each plugin must extend this controller in
 * as controllers/lli_{plugin_name}_controller.php
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil.controllers
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::uses('Controller', 'Controller');
App::uses('LilPluginRegistry', 'Lil.Lib');
/**
 * LilPluginController class
 *
 * @uses          AppController
 * @package       lil
 * @subpackage    lil.controllers
 */
class LilPluginController extends Controller {
/**
 * name property
 *
 * @var string 'LilPlugin'
 * @access public
 */
	var $name = 'LilPlugin';
/**
 * autoRender property
 *
 * @var boolean
 * @access public
 */
	var $autoRender = NULL;
/**
 * autoLayout property
 *
 * @var boolean
 * @access public
 */
	var $autoLayout = NULL;
/**
 * uses property
 *
 * @var array
 * @access public
 */
	var $uses = NULL;
/**
 * components property
 *
 * @var array
 * @access public
 */
	var $components = array('Session', 'Cookie');
/**
 * handlers property
 *
 * @var array
 * @access public
 */
	var $handlers = array('initialize' => array('function' => '_initialize', 'params' => array()));
/**
 * __construct method
 *
 * @access public
 * @return void
 */
	function __construct($controller) {
		parent::__construct();
		
		// set current area
		App::uses('LilCurrentArea', 'Lil.Lib');
		$this->currentArea = LilCurrentArea::getInstance();
		
		// set current user
		App::uses('LilCurrentUser', 'Lil.Lib');
		$this->currentUser = LilCurrentUser::getInstance();
	}
/**
 * attachHandler method
 *
 * @param string $action
 * @param string $function_name
 * @param mixed $params
 * @access public
 * @return void
 */
	function attachHandler($action, $function_name, $params = array()) {
		$this->handlers[$action] = array('function' => $function_name, 'params' => (array)$params);
	}
/**
 * insertIntoArray method
 *
 * Insert a new element into array
 *
 * @param array $dest
 * @param array $element
 * @param array $options
 * @access public
 * @return void
 */
	function insertIntoArray(&$dest, $element, $options) {
		if (isset($options['after']) || isset($options['replace'])) {
			$title = isset($options['after']) ? $options['after'] : $options['replace'];
			
			$panels = array_keys($dest);
			$i = 0;
			for ($i = 0; $i < sizeof($dest); $i++) {
				if ($panels[$i] == $title) break;
			}
			
			if (isset($panels[$i]) && $panels[$i] == $title) {
				if (isset($options['replace'])) { unset($dest[$title]); $i--; };
				
				// do this to preserve array keys
				$dest = 
					array_slice($dest, 0, $i + 1, true) + 
					$element + 
					array_slice($dest, $i + 1, sizeof($dest) - $i, true);
			}
		} else if (isset($options['before'])) {
			$panels = array_keys($dest);
			$i = 0;
			for ($i = 0; $i < sizeof($dest); $i++) {
				if ($panels[$i] == $options['before']) break;
			}
			
			if ($panels[$i] == $options['before']) {
				// do this to preserve array keys
				$dest = 
					array_slice($dest, 0, $i, true) + 
					$element + 
					array_slice($dest, $i, sizeof($dest) - $i, true);
			}
		} else if (isset($options['end'])) {
			$dest = $dest + $element;
		}
	}
/**
 * sidebarInsertPanel method
 *
 * Insert a new sidebar panel with specific options
 *
 * @param array $options
 * @param array $panel Sidebar panel
 * @access public
 * @return void
 */
	function sidebarInsertPanel(&$sidebar, $options, $panel) {
		$this->insertIntoArray($sidebar, $panel, $options);
	}
}